package 我的Java学习_算法基础.day_08;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author 挚爱之夕
 * @date 2021/8/23 - 08 - 23 - 16:18
 * @Description: 我的Java学习.day_08
 * @Version: 1.0
 */

/**
 * 用最少区间覆盖线段，求最少区间数
 * 这里特别地：（1，3）（4，5） 覆盖了（1，5）
 * 即只需覆盖整点，可以不覆盖整个区间
 * input:
 * 3 10    //三个区间，1-10
 * 1 7
 * 3 6
 * 6 10
 * output:
 * 2
 */
public class _126_覆盖区间点 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Interval[] intervals = new Interval[n];
        int end = sc.nextInt();
        for (int i = 0; i < n; i++) {
            intervals[i]=new Interval(sc.nextInt(),sc.nextInt());
        }
        Arrays.sort(intervals);
        f(intervals,end);
    }

    /**
     *
     * @param intervals
     * @param end
     */
    private static void f(Interval[] intervals,int end){
        int left = 1;//要覆盖的目标点
        int right = 1;
        int count =1;
        for (int i = 0; i < intervals.length; i++) {
            int l = intervals[i].left;
            int r = intervals[i].right;
            if(i==0&&l>1) break;
            if(l<=left){//当前区间可能覆盖left
                right = Math.max(right,r);//更新更右的端点
            }else{//开始下一个区间
                count++;
                left = right+1; //这里是覆盖整点，若覆盖区间则将right+1都改为right
                if(l>right+1)
                    break;
                else{
                    right = Math.max(right,r);
                }
            }

            if(right>=end) break;
        }
        if(right<end)
            System.out.println(-1);
        else
            System.out.println(count);
    }
    private static class Interval implements Comparable<Interval>{
        int left;
        int right;

        public Interval(int left, int right) {
            this.left = left;
            this.right = right;
        }

        //比较左端点
        @Override
        public int compareTo(Interval o) {
            int t = this.left - o.left;
            if(t==0)
                return this.right - o.right;
            return t;
        }
    }
}
